import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router/index.js'

// create an axios instance
// 通过axios.create 工厂函数创建请求实例
const service = axios.create({
  // 后台请求地址（根据环境变量动态配置）
  baseURL: process.env.VUE_APP_BASE_API // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  // 请求超时控制，不需要使用
  // timeout: 5000 // request timeout
})

// request interceptor
// 请求拦截器=》发请求之前执行
service.interceptors.request.use(
  config => {
    // do something before request is sent
    // 统一添加token
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Authorization'] = `Bearer ${store.getters.token}`
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
// 响应拦截器=》请求响应了（页面还没拿到数据）
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    // 正常：状态码200进入
    const { success, data, message } = response.data
    if (success) {
      return data
    } else {
      Message({
        message: message,
        type: 'error',
        duration: 3 * 1000
      })
      return Promise.reject(new Error(message))
    }
    // 返回之后，页面才能获取res数据
    // 简化返回给页面的数据
  },
  error => {
    // 出错：状态码400 500 进入
    if (error.response.status === 401) {
      if (router.currentRoute.path === '/login') return
      Message({
        message: error.response.data.message,
        type: 'error',
        duration: 5 * 1000
      })
      store.dispatch('user/logoutActions')
      router.replace(`/login?redirect=${router.currentRoute.path}`)
    }
    return Promise.reject(error)
  }
)

export default service
